package com.dadaguai.common;


import com.dadaguai.constants.ResponseCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

//要加上异常控制注解

@ControllerAdvice
public class GlobalExceptionHandler {

    @Order(Ordered.LOWEST_PRECEDENCE - 2) //设置业务异常优先级
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public R businessException(BusinessException e){
        // TODO: 2023/10/20 异常状态码判断
        e.printStackTrace();
        return R.error(e.getMessage(),e.getCode());
    }

    /**
     * 请求参数类型错误
     * @param e
     * @return
     */
    @ExceptionHandler({ MethodArgumentTypeMismatchException.class})
    /*如果不加，则会导致无法进行异常处理*/
    @ResponseBody
    public R numberFormaError(Exception e){
        e.printStackTrace();
        return R.error("请求参数类型错误", ResponseCode.PARAM_TYPE_ERROR);
    }


    @ExceptionHandler({  MissingServletRequestParameterException.class})
    /*如果不加，则会导致无法进行异常处理*/
    @ResponseBody
    public R missingRequestParamError(Exception e){
        e.printStackTrace();
        return R.error("请求参数错误",ResponseCode.PARAM_MISS_ERROR);
    }


    /**
     * 请求字段校验失败
     * @param e
     * @return
     */
    @ExceptionHandler({ ConstraintViolationException.class})
    @ResponseBody
    public R fieldCheckException(ConstraintViolationException e){
        e.printStackTrace();
        String[] split = e.getMessage().split(" ");
        return R.error(split[1],ResponseCode.PARAM_CHECK_ERROR);
    }
    /**
     * 参数校验异常
     * @param e
     * @return
     */
    @ExceptionHandler({ BindException.class})
    @ResponseBody
    public R BindException(BindException e){
        e.printStackTrace();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        List<String> collect = fieldErrors.stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList());
        for (String item:collect){
            System.out.println(item);
        }
        return R.error(collect.toString(),ResponseCode.PARAM_CHECK_ERROR);
    }

    /**
     * 全局异常处理，捕获未指定的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    /*如果不加，则会导致无法进行异常处理*/
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        return R.error("服务器异常，请联系管理员",ResponseCode.UNKNOWN_ERROR);
    }


}
